package com.client.gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;

import java.awt.Component;
import java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;
import java.awt.Window;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;



import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import javax.swing.ImageIcon;

public class Archiwum extends JFrame {

	private JPanel contentPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					String user = null;
					Archiwum frame = new Archiwum(user);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Archiwum(final String user) {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 355);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 47, 414, 196);
		contentPane.add(scrollPane_1);
		
		table = new JTable();
		table.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent arg0) {
				DefaultTableModel model=(DefaultTableModel) table.getModel();
		        model.setRowCount(0);
		        String sql="select * from notow as n join indexy as i using(id_index) where id_index =" +
		        		"(select id_index from uprawnienia as upr join user as u using(id_user) where username="+quotate(user)+")";          
		        try {			            
		            Class.forName("com.mysql.jdbc.Driver");
		            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
		            Statement stmt=con.createStatement();
		            ResultSet rs = stmt.executeQuery(sql);
		            while (rs.next())
		            {
		               
		                String data= rs.getString("data");
		                String wart= rs.getString("wartosc");
		                String nazwa= rs.getString("index_name");

		                model.addRow(new Object []{
		                    data, wart, nazwa
		                });			    
		            }
		        }
		        catch(Exception e) {
		           // JOptionPane.showMessageDialog(this, e.getMessage());
		        }
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Data", "Index", "Warto\u015B\u0107", "+/-"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, true, true, true
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setPreferredWidth(115);
		table.getColumnModel().getColumn(1).setPreferredWidth(86);
		table.getColumnModel().getColumn(3).setPreferredWidth(40);
		scrollPane_1.setViewportView(table);
		
		JLabel label = new JLabel("mClient");
		label.setForeground(new Color(0, 128, 128));
		label.setFont(new Font("Tahoma", Font.BOLD, 26));
		label.setBounds(314, 13, 110, 23);
		contentPane.add(label);
		
		JLabel lblArchiwumTwoichNotowa = new JLabel("Archiwum Twoich notowa\u0144");
		lblArchiwumTwoichNotowa.setForeground(SystemColor.textHighlight);
		lblArchiwumTwoichNotowa.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblArchiwumTwoichNotowa.setBounds(10, 17, 247, 19);
		contentPane.add(lblArchiwumTwoichNotowa);
		
		JLabel label_2 = new JLabel("<userName>");
		label_2.setBounds(158, 302, 64, 14);
		contentPane.add(label_2);
		label_2.setText(user);
		
		JLabel lblNewLabel = new JLabel("Zalogowany jako: ");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel.setBounds(71, 302, 88, 14);
		contentPane.add(lblNewLabel);
		
		JButton btnNewButton = new JButton("Wczytaj ponownie");
		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
		      
			}
		});
		btnNewButton.setBounds(71, 254, 127, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Wyczysc");
		btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DefaultTableModel tableModel=(DefaultTableModel) table.getModel();
				tableModel.setRowCount(0);
			}
		});
		btnNewButton_1.setBounds(208, 254, 89, 23);
		contentPane.add(btnNewButton_1);
		
		JButton btnNewButton_4 = new JButton("Zamknij program");
		btnNewButton_4.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		btnNewButton_4.setBounds(307, 284, 117, 23);
		contentPane.add(btnNewButton_4);
		
		JButton btnNewButton_2 = new JButton("Zamknij okno");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  Window frame = SwingUtilities.windowForComponent((Component) e
		                    .getSource());
		            frame.setVisible(false);
			}
		});
		btnNewButton_2.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_2.setBounds(307, 254, 117, 23);
		contentPane.add(btnNewButton_2);
		
		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setIcon(new ImageIcon("C:\\Users\\marmicc\\Desktop\\PRO\\Broker\\ico\\comp.png"));
		lblNewLabel_1.setBounds(0, 254, 61, 62);
		contentPane.add(lblNewLabel_1);
	}
public String quotate(String content){
	    
        return "'"+content+"'";
    }
}
